/**
 * 
 */
package org.vsg.stock.presentation;

import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.vsg.common.web.json.ListResult;
import org.vsg.common.web.json.processor.JsDateViewJsonValueProcessor;
import org.vsg.common.web.servlet.view.ForwardView;
import org.vsg.stock.basic.domain.RealStockInfo;
import org.vsg.stock.basic.domain.StockDailyLine;
import org.vsg.stock.basic.domain.StockDayIndicator;
import org.vsg.stock.basic.domain.StockInfo;
import org.vsg.stock.basic.domain.StockPerformance;
import org.vsg.stock.basic.service.StockCategoryService;
import org.vsg.stock.basic.service.StockDailyLineService;
import org.vsg.stock.basic.service.StockDayIndicatorService;
import org.vsg.stock.basic.service.StockInfoService;
import org.vsg.stock.basic.service.StockPerformanceService;
import org.vsg.stock.presentation.voprocessor.StockPerformanceDefaultValueProcessor;

/**
 * @author vison
 *
 */
@Controller
public class StockController {

	private final static Logger logger = LoggerFactory.getLogger(StockController.class);	

	private final static String MODULE_NAME = "stock";	
	
	@Resource
	private StockCategoryService stockCategoryService;
	
	@Resource
	private StockInfoService stockInfoService;
	
	@Resource
	private StockDailyLineService stockDailyLineService;
	
	@Resource
	private StockDayIndicatorService stockDayIndicatorService;
	
	@Resource
	private StockPerformanceService stockPerformanceService; 
	
	
	@RequestMapping(value="/stock/stockWhow")	
	public ModelAndView toAppAction(@PathVariable String moduleCtx , @PathVariable String module , @PathVariable String action ) throws Exception {
		
		StringBuilder relaPath = new StringBuilder("/"+module);
		relaPath.append("/");
		relaPath.append(action+".do");
		
		ForwardView irv = new ForwardView( relaPath.toString() );
		
		ModelAndView mav = new ModelAndView(irv);
		return mav;
	}

	@RequestMapping(value="/stock/dailyKLine.do")	
	public ModelAndView dailyKLineHandle(String stockCode) throws Exception {
		
		StockInfo stockInfo = stockInfoService.loadStockInfo(stockCode);
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/stock/stock-daily-kline");	// view name		
		
		mav.addObject("stockCode", stockInfo.getCode());
		mav.addObject("stockName", stockInfo.getName());
		
		return mav;
	}	
	
	@RequestMapping(value="/stock/showStockCategory.do")	
	public ModelAndView showStockCategory(String categoryId) throws Exception {
		
		ModelAndView mav = new ModelAndView();
        mav.setViewName("/stock/stock-category-show");	// view name
        mav.addObject("categoryId", categoryId);
        // --- convert model to data format
		JsonConfig jcfg = new JsonConfig();

		return mav;
	}

	@RequestMapping(value="/stock/listAStockfinaceflowing.do")	
	public ModelAndView listAStockfinaceflowing(String stockCode) throws Exception {
		
		List<StockDayIndicator> viewList  = stockDayIndicatorService.findAllDayIndicator(stockCode);
		
		
		// ----- add map to json object ----
		ListResult lr = new ListResult();
		lr.setResults(viewList.size());
		lr.setRows(viewList);
		
		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("json");	// view name
        // --- convert model to data format
		JsonConfig jcfg = new JsonConfig();
		jcfg.registerJsonValueProcessor(java.util.Date.class,new JsDateViewJsonValueProcessor());
		JSONObject jsonObj = JSONObject.fromObject(lr, jcfg);        
        mav.addObject("json",jsonObj);
        
        logger.info(jsonObj.toString()); 		
		
		return mav;
	}	

	@RequestMapping(value="/stock/xmlAmStockSettings.do")	
	public ModelAndView xmlAmStockSettings(String stockCode) throws Exception {
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/stock/xml_amstock_settings");	// view name
		
		mav.addObject("stockCode", stockCode);		

		return mav;
	}

	
	@RequestMapping(value="/stock/csvStockData.do")	
	public ModelAndView csvStockData(String stockCode) throws Exception {
		
		
		List<StockDailyLine> sdlLine = stockDailyLineService.findStockLineByAll(stockCode);
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/stock/stockdata_tpl");	// view name		
		mav.addObject("dailyList", sdlLine);	
		return mav;
	}
	
	
	
	@RequestMapping(value="/stock/listStocksOfCategory.do")	
	public ModelAndView listStocksOfCategory(int categoryId) throws Exception {
		
		List<RealStockInfo> viewList  = stockInfoService.getAssoStockInfoByCategoryId(categoryId);
		
		// ----- add map to json object ----
		ListResult lr = new ListResult();
		lr.setResults(viewList.size());
		lr.setRows(viewList);
		
		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("json");	// view name
        // --- convert model to data format
		JsonConfig jcfg = new JsonConfig();
		jcfg.setExcludes(new String[] {
			"assoStockCategory"	
		});
		//jcfg.registerJsonValueProcessor(java.sql.Timestamp.class,new JsTimestampJsonValueProcessor());
		JSONObject jsonObj = JSONObject.fromObject(lr, jcfg);        
        mav.addObject("json",jsonObj);
        
        logger.info(jsonObj.toString()); 
		return mav;
	}	

	
	@RequestMapping(value="/"+MODULE_NAME+"/{stkCode}/performance.do" , method=RequestMethod.GET)	
	public ModelAndView performance(@PathVariable String stkCode) throws Exception {
		
		List<StockPerformance> viewList  = stockPerformanceService.findPerformanceByStockCode(stkCode);
		
		// ----- add map to json object ----
		ListResult lr = new ListResult();
		lr.setResults(viewList.size());
		lr.setRows(viewList);
		
		/**
		 * defined the module and view handle
		 */
		ModelAndView mav = new ModelAndView();
        mav.setViewName("json");	// view name
        // --- convert model to data format
		JsonConfig jcfg = new JsonConfig();
		jcfg.registerJsonValueProcessor(java.util.Date.class,new JsDateViewJsonValueProcessor());
		jcfg.registerDefaultValueProcessor( Double.class, new StockPerformanceDefaultValueProcessor() );

		
		JSONObject jsonObj = JSONObject.fromObject(lr, jcfg);        
		mav.addObject("json",jsonObj);
        
        logger.info(jsonObj.toString()); 
		return mav;
	}	

	@RequestMapping(value="/"+MODULE_NAME+"/performanceView.do", method=RequestMethod.GET)	
	public ModelAndView performanceView(String stkCode) throws Exception {
		
		ModelAndView mav = new ModelAndView();
        mav.setViewName("/stock/stock-performance");	// view name
        // --- convert model to data format
		JsonConfig jcfg = new JsonConfig();
		
		mav.addObject("stockCode", stkCode);
		//mav.addObject("stockName", stockInfo.getName());
		return mav;
	}
	
}
